package com.taptech.doufu.ui.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.klinker.android.link_builder.Link;
import com.klinker.android.link_builder.LinkBuilder;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.MarketNovelNoticeBean;
import com.taptech.doufu.bean.MsgBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.manager.UserLogManager;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.ui.activity.SpeakerWallActivity;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.util.TMAnalysis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AutoPollAdapter extends RecyclerView.Adapter<AutoPollAdapter.ViewHolder> {
    private final Context mContext;

    private String pageType;
    private StringBuilder eventName = new StringBuilder();
    private final List<MarketNovelNoticeBean> mData;
    public AutoPollAdapter(Context context, List<MarketNovelNoticeBean> list,String pageType) {
        this.mContext = context;
        this.mData = list;
        this.pageType = pageType;
        if (!TextUtils.isEmpty(pageType)) {
            this.eventName.append(pageType).append("-").append("trumpert");
        }else{
            this.eventName.append("trumpert");
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_auto_poll, parent, false);
        ViewHolder holder = new ViewHolder(view);
        holder.tvContent = view.findViewById(R.id.tvContent);
        return holder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final MarketNovelNoticeBean data = mData.get(position%mData.size());
        holder.tvContent.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        String textColor ;
        if (data.getObject_type().equals("61")) {//speaker wall
            List<Link> links = new ArrayList<>();
            List<MsgBean> lists = MsgBean.getMsgBean(data.getMsg());
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < lists.size(); i++) {
                MsgBean msgBean = lists.get(i);
                stringBuilder.append(msgBean.getSplit_text());
                if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                    textColor = msgBean.getNight_color();
                } else {
                    textColor = msgBean.getDay_color();
                }
                if (msgBean.getUid() != null) {
                    links.add(setLink(msgBean.getUid(), msgBean.getSplit_text(), textColor, false));
                } else {
                    links.add(setLink(data.getId(), msgBean.getSplit_text(), textColor, true));
                    holder.tvContent.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            umengEvent();
                            UserLogManager.addLog(eventName.toString());
                            startLinkSpeakerWall(data.getId());
                        }
                    });
                }
            }
            holder.tvContent.setText(stringBuilder.toString());
            LinkBuilder.on(holder.tvContent).addLinks(links).build();
        } else {
            List<MsgBean> lists = MsgBean.getMsgBean(data.getMsg());
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < lists.size(); i++) {
                MsgBean msgBean = lists.get(i);
                stringBuilder.append(msgBean.getSplit_text());
            }
            holder.tvContent.setText(stringBuilder.toString());
            if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.text_dark));
            } else {
                holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.text_color_8993A3));
            }
            holder.tvContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    umengEvent();
                    //跳转到小说详情
                    MarketNovelNoticeBean.ObjectBean bean = data.getData();
                    if (bean != null) {
                        UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_NOVEL_DETAIL,String.valueOf(bean.getId()),
                                "小喇叭", pageType,"TFNovelDetail.js");
                        goToNovelDesActivity(bean.getId() + "", bean.getObject_type() + "", bean.getTitle());
                    }
                }
            });
        }
    }
    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }


    private Link setLink(final String uuid, final String userName, String color, final boolean isWall) {
        Link link = new Link(userName);
        link.setUnderlined(false);
        link.setTextColor(Color.parseColor("#" + color));
        link.setOnClickListener(new Link.OnClickListener() {
            @Override
            public void onClick(String clickedText) {
                umengEvent();
                UserLogManager.addLog(eventName.toString());
                if (isWall) {//to speaker wall
                    startLinkSpeakerWall(uuid);
                } else {// to people page
                    startLinkPerson(uuid);
                }

            }
        });
        return link;
    }


    private void goToNovelDesActivity(String novelId, String novelType, String fromTitleName) {
        TMAnalysis.event(mContext, "novel_laba_click");
        NovelDetailsActivity.Companion.startActivity(mContext,novelId, Intent.FLAG_ACTIVITY_NEW_TASK);
        // 统计事件
        Map<String, String> analysisMap = new HashMap<>();
        analysisMap.put("mpositon", "0");
        analysisMap.put("mid", novelId);
        analysisMap.put("mtype", novelType);
        TMAnalysis.event(mContext, fromTitleName, analysisMap);
    }


    private void startLinkSpeakerWall(String id) {
        Intent intent = new Intent(mContext, SpeakerWallActivity.class);
        intent.putExtra(Constant.RECORDID, id);
        mContext.startActivity(intent);
    }

    private void startLinkPerson(String uuid) {
        UserDetailActivity.Companion.startActivity(mContext,uuid);
    }

    private void umengEvent() {
        Map<String, String> analysisMap = new HashMap<String, String>();
        analysisMap.put("page_type",pageType);
        TMAnalysis.event(mContext, UmengEventName.HOME_SMALL_HORN, analysisMap);
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvContent;
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}